﻿using global::Mapster;
using System;
using System.Reflection;
namespace Project.Frame.Net.Common.Mapster
{
    public static class MapperHelper
    {
        private static bool _isInitialized;

        /// <summary>
        /// 初始化全局映射配置
        /// </summary>
        public static void Initialize(Action<TypeAdapterConfig>? configAction = null)
        {
            if (_isInitialized) return;

            var config = TypeAdapterConfig.GlobalSettings;

            // 自动扫描程序集中的所有映射配置（实现 IRegister 的类）
            config.Scan(Assembly.GetExecutingAssembly()); // 根据实际情况替换程序集

            // 可选：自定义全局配置
            config.Default.PreserveReference(true);

            configAction?.Invoke(config);

            _isInitialized = true;
        }

        /// <summary>
        /// 映射到新对象
        /// </summary>
        public static TTarget Map<TSource, TTarget>(TSource source)
            => source.Adapt<TTarget>();

        /// <summary>
        /// 映射到现有对象
        /// </summary>
        public static TTarget MapTo<TSource, TTarget>(TSource source, TTarget target)
            => source.Adapt(target);
    }


}
